local extension = Package:new("wq_buxiu")
extension.extensionName = "FkWqmt"
Fk:loadTranslationTable{
  ["wq_buxiu"] = "无期迷途·不朽",
}

local Wqc = require "packages/FkWqmt/wq_common"

-- 禁闭者：曼缇斯
local wq_mantisi = General:new(extension, "wq_mantisi", "qun", 3, 3, General.Female)
-- 技能：掠影
local wq_skill_lueying = fk.CreateActiveSkill{
  name = "wq_skill_lueying",
  switch_skill_name = "wq_skill_lueying",
  anim_type = "switch",
  card_num = function(self)
    if Self:getSwitchSkillState(self.switchSkillName, false) == fk.SwitchYang then
      return 1
    else
      return 0
    end
  end,
  target_num = 1,
  prompt = function(self)
    if Self:getSwitchSkillState(self.switchSkillName, false) == fk.SwitchYang then
      return "#wq_skill_lueying_prompt_yang"
    else
      return "#wq_skill_lueying_prompt_yin"
    end
  end,
  can_use = function(self, player)
    if player:getSwitchSkillState(self.switchSkillName, false) == fk.SwitchYang then
      return player:getMark(self.name.."_yang-phase") == 0 and not player:isKongcheng()
    else
      return player:getMark(self.name.."_yin-phase") == 0
    end
  end,
  card_filter = function(self, to_select, selected)
    if Self:getSwitchSkillState(self.switchSkillName, false) == fk.SwitchYang then
      return #selected == 0 and (not Self:prohibitDiscard(Fk:getCardById(to_select)))
    else
      return false
    end
  end,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id
  end,
  on_use = function (self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local state = player:getSwitchSkillState(self.switchSkillName, true, true)
    room:setPlayerMark(player, self.name.."_"..state.."-phase", 1)
    if player:getSwitchSkillState(self.switchSkillName, true) == fk.SwitchYang then
      local card = effect.cards[1]
      room:throwCard(card, self.name, player, player)
      if not player.dead and not target.dead then
        room:useVirtualCard("dismantlement", nil, player, target, self.name, true)
      end
    else
      room:drawCards(player, 1, self.name)
      if not player.dead and not target.dead then
        room:useVirtualCard("snatch", nil, player, target, self.name, true)
      end
    end
  end,
}
-- 技能：猎手
local wq_skill_lieshou = fk.CreateTriggerSkill{
  name = "wq_skill_lieshou",
  anim_type = "special",
  events = {fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target == player and player.phase == Player.Play and (data.card.type == Card.TypeBasic or data.card:isCommonTrick()) and #AimGroup:getAllTargets(data.tos) == 1 then
      local room = player.room
      local dest = room:getPlayerById(data.to)
      local lastSeat = player:getMark("wq_skill_lieshou_mark_seat-phase")
      if lastSeat > 0 then
        self.cost_data = dest
        return dest.seat ~= lastSeat
      else
        room:setPlayerMark(player, "wq_skill_lieshou_mark_seat-phase", dest.seat)
        return false
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local dest = self.cost_data
    local lastSeat = player:getMark("wq_skill_lieshou_mark_seat-phase")
    if dest.seat > lastSeat and player:isWounded() then
      if room:askForSkillInvoke(player, self.name, data, "#wq_skill_lieshou_prompt-increment") then
        return true
      end
    elseif dest.seat < lastSeat then
      if room:askForSkillInvoke(player, self.name, data, "#wq_skill_lieshou_prompt-decrement::"..dest.id) then
        return true
      end
    end
    room:setPlayerMark(player, "wq_skill_lieshou_mark_seat-phase", dest.seat)
    return false
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dest = room:getPlayerById(data.to)
    local lastSeat = player:getMark("wq_skill_lieshou_mark_seat-phase")
    if dest.seat > lastSeat then
      room:recover {
        who = player,
        num = 1,
        recoverBy = player,
        skillName = self.name
      }
    elseif dest.seat < lastSeat then
      room:damage{
        from = player,
        to = dest,
        damage = 1,
        skillName = self.name,
      }
    end
    if player:isAlive() then
      room:setPlayerMark(player, "wq_skill_lieshou_mark_seat-phase", dest.seat)
    end
  end,
}
-- 曼缇斯技能
wq_mantisi:addSkill(wq_skill_lueying)
wq_mantisi:addSkill(wq_skill_lieshou)
-- 曼缇斯翻译
Fk:loadTranslationTable{
  ["wq_mantisi"] = "曼缇斯",
  ["#wq_mantisi"] = "生物拟态",
  ["designer:wq_mantisi"] = "妙霄",
  ["cv:wq_mantisi"] = "官方",
  ["illustrator:wq_mantisi"] = "官方",
  ["wq_skill_lueying"] = "掠影",
  [":wq_skill_lueying"] = "转换技，阳：出牌阶段限一次，你可以弃置一张牌并指定一名其他角色，视为对其使用一张【过河拆桥】；阴：出牌阶段限一次，你可以摸一张牌并指定一名其他角色，视为对其使用一张【顺手牵羊】。",
  ["$wq_skill_lueying"] = "抓到你啦！",
  ["#wq_skill_lueying_prompt_yang"] = "掠影：你可以弃置一张牌并指定一名其他角色，视为对其使用一张【过河拆桥】",
  ["#wq_skill_lueying_prompt_yin"] = "掠影：你可以摸一张牌并指定一名其他角色，视为对其使用一张【顺手牵羊】",
  ["wq_skill_lieshou"] = "猎手",
  [":wq_skill_lieshou"] = "出牌阶段，当你使用基本牌或普通锦囊牌指定唯一目标后，若其座位号与你此阶段内以此法指定的上一个目标的座位号：1.呈递增关系，你可以回复1点体力；2.呈递减关系，你可以对其造成1点伤害。",
  ["$wq_skill_lieshou"] = "弱肉强食就是自然的法则。",
  ["#wq_skill_lieshou_prompt-increment"] = "猎手：你可以回复1点体力",
  ["#wq_skill_lieshou_prompt-decrement"] = "猎手：你可以对%dest造成1点伤害",
  ["~wq_mantisi"] = "啊——",
}

-- 禁闭者：德雷雅
local wq_deleiya = General:new(extension, "wq_deleiya", "qun", 3, 3, General.Female)
-- 技能：星震
local wq_skill_xingzhen = fk.CreateActiveSkill{
  name = "wq_skill_xingzhen",
  anim_type = "control",
  card_num = 0,
  target_num = 1,
  prompt = function(self)
    return "#wq_skill_xingzhen_prompt"
  end,
  can_use = function(self, player)
    return not player:isKongcheng() and player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected, selected_cards)
    local target = Fk:currentRoom():getPlayerById(to_select)
    return #selected == 0 and Self:canPindian(target) and Self:inMyAttackRange(target)
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local pindian = player:pindian({target}, self.name)
    if pindian.results[target.id].winner == player then
      if target:isAlive() and (not target:isNude()) then
        local card = room:askForCardChosen(player, target, "he", self.name)
        room:obtainCard(player, card, false, fk.ReasonPrey)
      end
      if target:isAlive() then
        room:addPlayerMark(target, "@wq_mark_liuxue", 1)
      end
      if player:isAlive() then
        player:setSkillUseHistory(self.name, 0, Player.HistoryPhase)
      end
    end
  end,
}
-- 技能：自旋
local wq_skill_zixuan = fk.CreateTriggerSkill{
  name = "wq_skill_zixuan",
  anim_type = "control",
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target ~= player and target:getMark("@wq_mark_liuxue") > 0
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, data, "#wq_skill_zixuan_prompt:"..target.id)
  end,
  on_use = function(self, event, target, player, data)
    Wqc.doLiuxueJudge(target)
  end,
}
-- 技能：喧嚣
local wq_skill_xuanxiao = fk.CreateActiveSkill{
  name = "wq_skill_xuanxiao",
  switch_skill_name = "wq_skill_xuanxiao",
  anim_type = "switch",
  card_num = 0,
  target_num = 1,
  prompt = function(self)
    if Self:getSwitchSkillState(self.switchSkillName, false) == fk.SwitchYang then
      return "#wq_skill_xuanxiao_prompt_yang"
    else
      return "#wq_skill_xuanxiao_prompt_yin:::"..math.min(Self:getMark("wq_skill_xuanxiao_mark-obtain"), 3)
    end
  end,
  can_use = function(self, player)
    if player:getSwitchSkillState(self.switchSkillName, false) == fk.SwitchYang then
      return player:getMark(self.name.."_yang-phase") == 0
    else
      return player:getMark(self.name.."_yin-phase") == 0 and player:getMark("wq_skill_xuanxiao_mark-obtain") > 0
    end
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected)
    if Self:getSwitchSkillState(self.switchSkillName, false) == fk.SwitchYang then
      return #selected == 0 and to_select ~= Self.id and Fk:currentRoom():getPlayerById(to_select):getMark("@wq_mark_liuxue") > 0
    else
      return #selected == 0 and to_select ~= Self.id
    end
  end,
  on_use = function (self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local state = player:getSwitchSkillState(self.switchSkillName, true, true)
    room:setPlayerMark(player, self.name.."_"..state.."-phase", 1)
    if player:getSwitchSkillState(self.switchSkillName, true) == fk.SwitchYang then
      local num = math.min(target:getMark("@wq_mark_liuxue"), 3)
      room:drawCards(player, num, self.name)
      Wqc.doLiuxueJudge(target)
    else
      local num = math.min(player:getMark("wq_skill_xuanxiao_mark-obtain"), 3)
      room:addPlayerMark(target, "@wq_mark_liuxue", num)
    end
  end,
}
local wq_skill_xuanxiao_obtain = fk.CreateTriggerSkill{
  name = "#wq_skill_xuanxiao_obtain",
  mute = true,
  refresh_events = {fk.AfterCardsMove, fk.EventPhaseEnd},
  can_refresh = function(self, event, target, player, data)
    if event == fk.AfterCardsMove then
      if player:hasSkill(self) and player.phase == Player.Play then
        self.cost_data = 0
        for _, move in ipairs(data) do
          if move.to == player.id and move.toArea == Card.PlayerHand then
            self.cost_data = self.cost_data + #move.moveInfo
          end
        end
        if self.cost_data > 0 then
          return true
        end
      end
    else
      return target == player and player:isAlive() and player.phase == Player.Play
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event ==  fk.AfterCardsMove then
      room:addPlayerMark(player, "wq_skill_xuanxiao_mark-obtain", self.cost_data)
    else
      room:setPlayerMark(player, "wq_skill_xuanxiao_mark-obtain", 0)
    end
  end,
}
wq_skill_xuanxiao:addRelatedSkill(wq_skill_xuanxiao_obtain)
-- 德雷雅的技能
wq_deleiya:addSkill(wq_skill_xingzhen)
wq_deleiya:addSkill(wq_skill_zixuan)
wq_deleiya:addSkill(wq_skill_xuanxiao)
-- 德雷雅的翻译
Fk:loadTranslationTable{
  ["wq_deleiya"] = "德雷雅",
  ["#wq_deleiya"] = "暗色坠星",
  ["designer:wq_deleiya"] = "妙霄",
  ["cv:wq_deleiya"] = "官方",
  ["illustrator:wq_deleiya"] = "官方",
  ["wq_skill_xingzhen"] = "星震",
  [":wq_skill_xingzhen"] = "出牌阶段限一次，你可以与攻击范围内的一名其他角色拼点。若你赢，你获得其一张牌，其获得1枚「流血」标记，然后你令此技能视为此阶段内未发动过。"
  ..Wqc.getTranslateInNewLine("#wq_common_mark_liuxue_description"),
  ["$wq_skill_xingzhen"] = "它不会回应你，只会将你撕碎，吞没……",
  ["#wq_skill_xingzhen_prompt"] = "星震：你可以与攻击范围内的一名其他角色拼点<br>若你赢，你获得其一张牌，其获得1枚「流血」标记，然后你令此技能视为此阶段内未发动过",
  ["wq_skill_zixuan"] = "自旋",
  [":wq_skill_zixuan"] = "其他角色的回合结束时，若其有「流血」标记，你可以令其进行“流血判定”。",
  ["$wq_skill_zixuan"] = "打破所有的桎梏，窥视我最深处的秘密。",
  ["#wq_skill_zixuan_prompt"] = "自旋：你可以令%src进行“流血判定”",
  ["wq_skill_xuanxiao"] = "喧嚣",
  [":wq_skill_xuanxiao"] = "转换技，阳：出牌阶段限一次，你可以指定一名有「流血」标记的其他角色。若如此做，你摸X张牌，然后令其进行“流血判定”（X为该角色拥有的「流血」标记数且至多为3）；阴：出牌阶段限一次，你可以令一名其他角色获得Y枚「流血」标记（Y为此阶段内你获得的牌数且至多为3）。",
  ["$wq_skill_xuanxiao"] = "赤色的光热将吞没一切！",
  ["#wq_skill_xuanxiao_prompt_yang"] = "喧嚣：你可以指定一名有「流血」标记的其他角色并摸X张牌，然后令其进行“流血判定”<br>（X为该角色拥有的「流血」标记数且至多为3）",
  ["#wq_skill_xuanxiao_prompt_yin"] = "喧嚣：你可以令一名其他角色获得%arg枚「流血」标记",
  ["~wq_deleiya"] = "星辰……终将湮灭于黑暗。",
}

return extension